const app = getApp();
const s = new app.Shuju();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    nian: app.Day().get().split('-')[0],
    yue: app.Day().get().split('-')[1],
    day: ['日', '一', '二', '三', '四', '五', '六'],
    rili: [],
    shitu: 1,
    qiurili: [],
    show: false,
    biaoti: '',
    neirong: '',
    shichang:0,
    today:""
  },

  hideModal(e) {
    this.setData({
      show: false
    })
  },

  _Shang: function () {
    let yue = ~~this.data.yue;
    let nian = ~~this.data.nian;
    if (yue == 1) {
      yue = 12;
      nian = nian - 1;
    } else {
      yue = yue - 1;
    }
    if (yue < 10) {
      yue = "0" + "" + yue;
    }
    this.setData({
      nian: nian,
      yue: yue,
      rcarr: []
    });
    this._Xieru();
  },

  _Xia: function () {
    let yue = ~~this.data.yue;
    let nian = ~~this.data.nian;
    if (yue == 12) {
      yue = 1;
      nian = nian + 1;
    } else {
      yue = yue + 1;
    }
    if (yue < 10) {
      yue = "0" + "" + yue;
    }
    this.setData({
      nian: nian,
      yue: yue,
      rcarr: []
    });
    this._Xieru();
  },


  _Xieru: function () {
    //获取本月的第一天是周几
    let nian = ~~this.data.nian;
    let yue = this.data.yue;
    let riqi = nian + '-' + yue + '-' + '01';
    let inweek = app.Day().week(riqi);
    //然后往前倒退，获得第一个的日期
    let firstriqi = app.Day().which(riqi, -inweek);
    if (inweek == 7) {
      firstriqi = riqi; // 如果他就是第一天。。。那么就直接处理
    }

    let dayarr = [];
    for (let i = 0; i < 31; i++) {
      let day = app.Day().which(riqi, i);
      // 是不是本月的日期
      if (day.split('-')[1] != yue) {
        break;
      } else {
        dayarr.push(`'${day}'`);
      }
    }

    console.log(dayarr)

    let that = this;
    s.si('uid');
    s.se('day', dayarr.join(','));
    s.g('p/getdaka', {
      s: function (data) {
        if (data.code == 200) {
          let dk = data.data.map((o)=>{
            return o.day;
          })
          //然后从这一天开始，循环35次，写入到arr中
          let arr = [];
          let l = 35;
          for (let i = 0; i < l; i++) {
            let day = app.Day().which(firstriqi, i);
            let by = true; // 是不是本月的日期
            if (day.split('-')[1] != yue) {
              by = false;
            }
            arr.push({
              day: day,
              num: ~~day.split('-')[2],
              by: by,
              qd: dk.indexOf(day)
            });
            if (i == 34) {
              //检查，这一天是不是这个月的最后一天
              let tomorrow = app.Day().which(day, 1);
              if (tomorrow.split('-')[1] == day.split('-')[1]) {
                if (tomorrow.split('-')[1] == yue) {
                  l = l + 7;
                }
              }
            }
          };


          console.log(arr)
          that.setData({
            rili: arr
          })
        }
      }
    })



  },

  _Jilu: function (e) {
    let day = e.currentTarget.dataset.for;
    s.se("day",day);
    s.si("uid");
    s.g("p/getshichang",{
      s:(d)=>{
        if(d.code == 200){
          this.setData({
            today:day,
            shichang:d.data.shichang,
            show:true
          })
        }else{
          this.setData({
            show:false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._Xieru();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})